import { ref } from "vue";
import { defineStore } from "pinia";
import { NumberMul } from "../libs/math.js";
import { setStore } from "@/libs/storage";

export const useMainStore = defineStore("main", () => {
  // State
  const islogin = ref(false);
  const FilePath = ref(import.meta.env.VITE_FILE_PATH);
  const SupplierFilePath = ref(import.meta.env.VITE_SUPPLIER_FILE_PATH);
  const baseUrl = ref(import.meta.env.VITE_BASE_URL);
  const PhotoPath = ref(import.meta.env.VITE_PHOTO_PATH); // 暂无图片默认图片
  const AdminPath = ref(import.meta.env.VITE_ADMIN_PATH);
  const AdminLabsciPath = ref(import.meta.env.VITE_ADMIN_LABSCI_PATH);
  const stkNos = ref(""); // 提交订单商品信息
  const RouterToIndexMethod = ref(null);

  // Actions
  function setStkNos({ type, stkNos: newStkNos }) {
    setStore("stkNos_" + type, newStkNos);
    stkNos.value = newStkNos;
  }

  function setLoginRouter(vm) {
    RouterToIndexMethod.value = setTimeout(() => {
      vm.$router.push({
        path: "/index",
      });
    }, 3000);
  }

  function clearLoginTimeout() {
    clearTimeout(RouterToIndexMethod.value);
    RouterToIndexMethod.value = null;
  }

  // Getter (计算属性)
  const subtotalsum = (price, qty) => NumberMul(price, qty);

  return {
    // State
    islogin,
    token,
    CusID,
    FilePath,
    SupplierFilePath,
    baseUrl,
    PhotoPath,
    AdminPath,
    AdminLabsciPath,
    stkNos,
    RouterToIndexMethod,

    // Actions
    setStkNos,
    setLoginRouter,
    clearLoginTimeout,

    // Getter
    subtotalsum,
  };
});
